package com.itheima.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

//测试  条件查询
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class UserMapperTest2 {

    @Autowired
    private UserMapper mapper;

    //查询所有  select *from  user

    @Test
    public void testFindAll(){

        //queryWrapper 添加构造器，用于构造各种条件（where 分组 排序 过滤） 泛型是操作的类
        QueryWrapper<User> QueryWrapper = new QueryWrapper<>();
        List<User> users = mapper.selectList(QueryWrapper);

        for (User user : users) {
            System.out.println(user);
        }
    }


    //不推荐使用
    @Test
    public void testFind1(){
        //创建构造器
        QueryWrapper<User> Wrapper = new QueryWrapper<>();

        //条件参数
        Integer ageparam = 10;

        //判断条件参数是否为空
        if(ageparam!=null){

            //往构造器中加入参数及判断逻辑
           Wrapper.gt("age",ageparam);

        }

        //调用mybatisplus提供的查询所有方法，并把构造器对象作为查询条件传入
        List<User> users = mapper.selectList(Wrapper);

        for (User user : users) {

            System.out.println(user);
        }
    }

    //推荐使用的条件构造器的方式
    @Test
    public void  testFind2(){

        //QueryWrapper 添加基于Lambda构造器，用于构造各种条件
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //现在这个值可能是null，可能是值  如果是null就不应该再添加age相关的条件
        Integer ageParam = null;

        //添加查询条件【参数一：表达式，只有这个表达式返回true的时候，后面的两个参数才生效
        // 二：实体类属性名称，参数三：具体的值】

        userLambdaQueryWrapper.gt(ageParam!=null,User::getAge,ageParam);

        List<User> users = mapper.selectList(userLambdaQueryWrapper);

        for (User user : users) {
            System.out.println(user);
        }

    }

    @Test
    public void testFind3(){
        //id       id>=1
        //name     name = "魂斗罗"
        //age      age  [10,40]
        //password    123%
        //tel      tel in "123","456","789"

        //1. 基本写法(不推荐)
//        //封装查询条件
//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
//        wrapper.ge(User::getId, 1);//id>=1
//        wrapper.eq(User::getName, "魂斗罗");//name = "魂斗罗"
//        wrapper.between(User::getAge, 10, 40);// age between 10 and 40
//        wrapper.likeRight(User::getPassword, "123"); //password like '123%'
//        wrapper.in(User::getTel, "123", "456", "789");  // tel in ("123","456","789")
//        //执行查询
//        List<User> userList = userMapper.selectList(wrapper);

        //2.链式编程（推荐）
        //封装查询条件

//        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        userLambdaQueryWrapper.ge(User::getId,1)
//                .eq(User::getName,"魂斗罗")
//                .between(User::getAge,10,40)
//                .likeRight(User::getPassword,"123")
//                .in(User::getTel,"123","456","789");
//
//        List<User> users = mapper.selectList(userLambdaQueryWrapper);
//
//        for (User user : users) {
//            System.out.println(user);
//        }

//        3.链式编程中使用or条件
        //封装查询条件
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.ge(User::getId,1)
                .or().eq(User::getName,"魂斗罗");
        List<User> users = mapper.selectList(userLambdaQueryWrapper);

        for (User user : users) {

            System.out.println(user);
        }
    }




}

























